<?php
require './vendor/autoload.php';
use setasign\Fpdi\Tcpdf\Fpdi;
use setasign\Fpdi\PdfReader;
use setasign\Fpdf\chinese;
/**
 *  FPDF:
 * 参考网站：https://blog.csdn.net/iteye_5904/article/details/82610548
 * $pdfpath PDF文件本地路径
 * $positiondata 签字位置数组array(array(页数，名字，X坐标，Y坐标))
 * $adminpath 签字图片地址数组array(签名，手章，公章)
*/
class AFPDFChajian extends Chajian{

    public function daorupdfifpdf($pdfpath, $positiondata=array(), $adminpath=array(), $orientation, $unit, $widthHeight){
        header("Content-type:text/html;charset=utf-8");

        if(empty($unit) && empty($widthHeight)) {
            $pdf = new FPDI($orientation, 'px');// 启动FPDI
        } else {
            $pdf = new FPDI($orientation, $unit, $widthHeight);// 启动FPDI
        }
        if(empty($pdfpath)) return false;
        $pdfpath = './'.$pdfpath;

        try {
            $pageCount   = $pdf->setSourceFile($pdfpath);
        } catch (Exception $e) {
            echo json_encode(
                array(
                    'code' => 300,
                    'msg' => '文件读取错误，请上传纯净的PDF文件',
                )
            );
            exit();
        }

        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++)
        {
            // 导入页面
            $templateId = $pdf->importPage($pageNo);
            $size = $pdf->getTemplateSize($templateId);
            //禁用自动分页符
            $pdf->SetAutoPageBreak(false);
            // 在新的 PDF 上新增一頁
            $pdf->addPage();
            // 使用导入的页面
            $pdf->useTemplate($templateId);

            $pdf->SetFont('stsongstdlight','','12');
            // // 签上你的名字
            // $pdf->SetXY($v['datax'], $v['datay']+10);
            // $pdf->Write(7, $v['dataname']);
            // // 带当前签名的日期
            // $pdf->SetXY($v['datax'], $v['datay']+20);
            // $pdf->Write(7, date('Y/m/d',time()));
            //电子印章
            //$pdf->image("./vendor/tecnickcom/tcpdf/examples/images/tcpdf_signature2.png", $v['datax'], $v['datay'], 50);

            if(!empty($positiondata) && count($positiondata)>0){
                foreach ($positiondata as $key=>$v){
                    if($pageNo==$v['page'])
                    {
                        if(isset($v['sign']) && $v['sign'] == true){
                            $pdf->image($adminpath['sign'], $v['signx'], $v['signy'],70,0,'PNG','');
                        }
                        if(isset($v['personal']) && $v['personal'] == true){
                            $pdf->image($adminpath['personal'], $v['personalx'], $v['personaly'], 100,0,'PNG','');
                        }
                        if(isset($v['seal']) && $v['seal'] == true){
                            $pdf->Image($adminpath['seal'], $v['sealx'], $v['sealy'], 118,118,'PNG','');
                        }
                    }
                }
            }
            // 最后一页签名
            if($pageNo==1)
            {   
                //$pdf->image("./upload/signature/2020-12/5fe56dad88860.png", 150, 225, 50);
                // $pdf->image("./vendor/tecnickcom/tcpdf/examples/images/130935256639637.png", 150, 225, 0);

                //$pdf->image("./vendor/tecnickcom/tcpdf/examples/images/tcpdf_signature2.png", 150, 225, 50);
            }
        }

        // 输出成本地端 PDF 檔案
        ob_clean();
        $pdf->output($_SERVER['DOCUMENT_ROOT'].trim($pdfpath, './'), "F");
        // 結束 FPDI 剖析器
//        $pdf->closeParsers();
        return $pdfpath;
    }

}                                                                                                                                                            